package com.bilibili.service.impl;

import java.util.List;

import com.bilibili.dao.CommentDao;
import com.bilibili.dao.impl.CommentDaoImpl;
import com.bilibili.dto.CommentInfo;
import com.bilibili.entity.Comment;
import com.bilibili.entity.Page;
import com.bilibili.entity.User;
import com.bilibili.service.CommentService;
import com.bilibili.service.UserService;

public class CommentServiceImpl implements CommentService {
	CommentDao cd = new CommentDaoImpl();
	@Override
	public int selectCountByVId(int videoId) {
		// TODO Auto-generated method stub
		return cd.selectCountByVId(videoId);
	}

	public List<Comment> selectAllByVId(int videoId) {
		return cd.selectAllByVId(videoId);
	}

	@Override
	public List<Comment> selectAllByUpId(int upId) {
		// TODO Auto-generated method stub
		return cd.selectAllByUpId(upId);
	}

	@Override
	public boolean deleteComment(int commentId) {
		return cd.deleteComment(commentId);
	}

	@Override
	public int selectAllCommentNum() {
		// TODO Auto-generated method stub
		return cd.selectAllCommentNum();
	}

	 private CommentDao commentDao = new CommentDaoImpl();
	    private UserService userService = new UserServiceImpl();
	    public int addComment(Comment comment) {
	        // 参数校验
	        if (comment == null) {
	            return 0;
	        }
	        
	        if (comment.getVideoId() <= 0) {
	            return 0;
	        }
	        
	        if (comment.getUserId() <= 0) {
	            return 0;
	        }
	        
	        if (comment.getContent() == null || comment.getContent().trim().isEmpty()) {
	            return 0;
	        }
	        
	        User user = userService.getUserById(comment.getUserId());
	        if (user != null) {
	            comment.setUsername(user.getUsername());
	            comment.setAvatarUrl(user.getAvatarUrl());
	        } else {
	           
	            comment.setUsername("未知用户");
	            comment.setAvatarUrl(user.getAvatarUrl());
	        }
	        Integer replyTo = comment.getReplyTo();
	     // 主评论设为NULL
	        if (replyTo == null || replyTo <= 0) {
	            comment.setReplyTo(null);
	        } else {
	            // 回复评论验证父评论存在
	            Comment parent = commentDao.selectCommentById(replyTo);
	            if (parent == null) {
	               System.out.println("外键不存在");
	                return 0;
	            }
	        }
	        
	        // 设置默认点赞数
	        if (comment.getLikeCount() < 0) {
	            comment.setLikeCount(0);
	        }
	        
	        return commentDao.insertComment(comment);
	    }

	    public List<Comment> getCommentsByVideoId(int videoId, int offset, int limit) {
	        if (videoId <= 0) {
	            return null;
	        }
	        return commentDao.selectCommentsByVideoId(videoId,offset,limit);
	    }

	    public List<Comment> getRepliesByParentId(int parentId) {
	        if (parentId <= 0) {
	            return null;
	        }
	        return commentDao.selectRepliesByParentId(parentId);
	    }

	    public Comment getCommentById(int commentId) {
	        if (commentId <= 0) {
	            return null;
	        }
	        return commentDao.selectCommentById(commentId);
	    }

	    public boolean likeComment(int commentId) {
	        if (commentId <= 0) {
	            return false;
	        }
	        
	        Comment comment = commentDao.selectCommentById(commentId);
	        if (comment == null) {
	            return false;
	        }
	        
	        int newLikeCount = comment.getLikeCount() + 1;
	        int rows = commentDao.updateCommentLikeCount(commentId, newLikeCount);
	        return rows > 0;
	    }
	    
	    
	    @Override
		public Page<CommentInfo> selectAllCommentInfoB(CommentInfo ci, int pageNum, int pageSize) {
			// TODO Auto-generated method stub
			List<CommentInfo> ciList = cd.selectAllCommentInfoB(ci, pageNum, pageSize);
			int totalRows = cd.selectAllCommentInfoBNum(ci);
			Page<CommentInfo> ciPage = new Page<CommentInfo>(ciList,pageNum,pageSize,totalRows);
			return ciPage;
		}
		@Override
		public boolean deleteCommentInfoByCId(Comment c) {
			// TODO Auto-generated method stub
			return cd.deleteCommentInfoByCId(c) == 1;
		}
}
